﻿using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using System.Windows.Forms;
using System;
using UtfprLibrary.EntityFramework.ComponentModel;
using UtfprLibrary.Windows.Forms.Helpers;

namespace UtfprLibrary.Windows.Forms.ComponentModel {
    /// <summary>
    /// TODO
    /// </summary>
    [DefaultBindingProperty("DataSource")]
    internal class UtfprBindingNavigatorActionList : UtfprActionListBase {
        #region Properties

        /// <summary>
        /// TODO
        /// </summary>
        private readonly UtfprBindingNavigatorBase linkedControl;

        /// <summary>
        /// TODO
        /// </summary>
        [TypeConverter("System.Windows.Forms.Design.AdvancedBindingTypeConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor("System.Windows.Forms.Design.DataSourceListEditor, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public object DataSource {
            get { return GetPropertyByName("DataSource").GetValue(linkedControl); }
            set { GetPropertyByName("DataSource").SetValue(linkedControl, value); }
        }

        /// <summary>
        /// TODO
        /// </summary>
        [DefaultValue("")]
        [TypeConverter("System.Windows.Forms.Design.DataMemberFieldConverter, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
        [Editor(typeof(UtfprBindingSourceDataMemberFieldEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public object KeyMember {
            get { return GetPropertyByName("KeyMember").GetValue(linkedControl); }
            set { GetPropertyByName("KeyMember").SetValue(linkedControl, value); }
        }

        /// <summary>
        /// TODO
        /// </summary>
        [TypeConverter(typeof(UtfprSourceNavigatorConverter))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public UtfprBindingNavigatorBase NavigatorSource {
            get { return (UtfprBindingNavigatorBase)GetPropertyByName("NavigatorSource").GetValue(linkedControl); }
            set { GetPropertyByName("NavigatorSource").SetValue(linkedControl, value); }
        }

        /// <summary>
        /// TODO
        /// </summary>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public FetchModeEnum FetchMode {
            get { return (FetchModeEnum)GetPropertyByName("FetchMode").GetValue(linkedControl); }
            set { GetPropertyByName("FetchMode").SetValue(linkedControl, value); }
        }
        
        /// <summary>
        /// TODO
        /// </summary>
        [Editor(typeof(UtfprEagerLoadEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public IUtfprRelatedEndDescription[] RelatedEndDescriptions {
            get { return (IUtfprRelatedEndDescription[])GetPropertyByName("RelatedEndDescriptions").GetValue(linkedControl); }
            set { GetPropertyByName("RelatedEndDescriptions").SetValue(linkedControl, value); }
        }

        #endregion

        /// <summary>
        /// TODO
        /// </summary>
        /// <param name="component"></param>
        /// <param name="designer"></param>
        public UtfprBindingNavigatorActionList(UtfprBindingNavigatorBase component, ControlDesigner designer)
            : base(component, designer) {
            linkedControl = (UtfprBindingNavigatorBase)LinkedControl;
            designTimeCreatingControlBehavior();
        }

        /// <summary>
        /// TODO
        /// </summary>
        /// <returns></returns>
        public override DesignerActionItemCollection GetSortedActionItems() {
            var items = new DesignerActionItemCollection();

            items.Add(new DesignerActionHeaderItem("Data"));

            items.Add(new DesignerActionPropertyItem(
                "DataSource", "Data Source", "Data", "Choose a Data Souce."));

            items.Add(new DesignerActionPropertyItem(
                "KeyMember", "Key Member", "Data", "Choose a Key Member."));

            items.Add(new DesignerActionPropertyItem(
                "FetchMode", "Fetch Mode", "Data", "Choose a Fetch Mode."));

            items.Add(new DesignerActionPropertyItem(
                "RelatedEndDescriptions", "RelatedEnd Descriptions", "Data", "Choose the entity collections to eager load."));

            //apenas o Details deve ter nenhum NavigatorSource
            if (linkedControl.CurrentMode == UBNModeEnum.Details) {
                items.Add(new DesignerActionPropertyItem(
                   "NavigatorSource", "Navigator Source", "Data", "Choose a UtfprBindingNavigator Source."));
            }

            return items;
        }

        /// <summary>
        /// When creating the control, checks whether the process is or not in Design-Time, 
        /// and then customize the initial behavior of UtfprBindingNavigator.
        /// </summary>
        private void designTimeCreatingControlBehavior() {
            try {
                if (linkedControl.CurrentMode == UBNModeEnum.None) {
                    var masterNavigator = UtfprBindingNavigatorBaseHelper.findRootSourceNavigator(linkedControl);
                    if (masterNavigator == null) {
                        linkedControl.CurrentMode = UBNModeEnum.Master;
                        linkedControl.Dock = DockStyle.Top;
                        linkedControl.QueryItem.UtfprControlVisible = true;
                        linkedControl.RefreshItem.UtfprControlVisible = true;
                        linkedControl.DuplicateItem.UtfprControlVisible = true;
                        UtfprBindingNavigatorBaseHelper.populateWithDetailsNavigators(linkedControl);
                    } else {
                        masterNavigator.UBNDetails.Add(linkedControl);
                        linkedControl.CurrentMode = UBNModeEnum.Details;
                        //linkedControl.Dock = DockStyle.None;
                        foreach (ToolStripItem item in linkedControl.Items) {
                            item.ImageScaling = ToolStripItemImageScaling.SizeToFit;
                        }
                        linkedControl.QueryItem.UtfprControlVisible = false;
                        linkedControl.RefreshItem.UtfprControlVisible = false;
                        linkedControl.DuplicateItem.UtfprControlVisible = false;
                    }
                } 
                //else if (linkedControl.CurrentMode == UBNModeEnum.Master) {
                //    linkedControl.QueryItem.UtfprControlVisible = true;
                //    linkedControl.RefreshItem.UtfprControlVisible = true;
                //    linkedControl.DuplicateItem.UtfprControlVisible = true;
                //} else if (linkedControl.CurrentMode == UBNModeEnum.Details) {
                //    foreach (ToolStripItem item in linkedControl.Items) {
                //        item.ImageScaling = ToolStripItemImageScaling.SizeToFit;
                //    }
                //    linkedControl.QueryItem.UtfprControlVisible = false;
                //    linkedControl.RefreshItem.UtfprControlVisible = false;
                //    linkedControl.DuplicateItem.UtfprControlVisible = false;
                //}
            } catch (Exception ex) {
                throw new ArgumentException("UTFPR: " + ex.Message);
            }
        }
    }
}
